package Servlet.AdminServlet;

import Entity.Admin;
import Service.AdminService.AdminService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/*
负责管理员 登陆 数据接口
 */
@WebServlet(value = "/adminLogin")
public class AdminLogin extends HttpServlet {

    private AdminService adminService = new AdminService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json; charset=utf-8");

        String adminId = request.getParameter("adminId");
        String adminPassword = request.getParameter("adminPassword");


        Admin admin = new Admin(adminId,adminPassword);

        admin = adminService.adminLogin(admin);
        

        PrintWriter out = response.getWriter();

        if(admin==null){
            out.print(0);
        }else{
            HttpSession session = request.getSession();
            session.setAttribute("admin",admin); //通行证
            out.print(1);
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
